package com.kc.web.controller.business;

import com.kc.business.domain.KcShareProfitConfig;
import com.kc.business.service.KcShareProfitConfigService;
import com.kc.common.annotation.Log;
import com.kc.common.annotation.RepeatSubmit;
import com.kc.common.constant.Constants;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.domain.model.LoginUser;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import com.kc.common.utils.ServletUtils;
import com.kc.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * FileName: KcShareProfitConfigController
 * Author:   TP
 * Date:     2020-11-03 19:58
 * Description:
 */
@RestController
@RequestMapping("business/shareProfitConfig")
public class KcShareProfitConfigController extends BaseController {

    @Autowired
    private KcShareProfitConfigService shareProfitConfigService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取配置信息列表
     */
    @GetMapping("/list")
    public TableDataInfo getList(KcShareProfitConfig config) {
        startPage();
        List<KcShareProfitConfig> list = shareProfitConfigService.getList(config);
        return getDataTable(list);
    }

    /**
     * 获取配置信息详情
     */
    @GetMapping("/info/{id}")
    public AjaxResult getInfo(@PathVariable Integer id) {
        if (id != null && id > 0) {
            return AjaxResult.success(shareProfitConfigService.getById(id));
        }
        return AjaxResult.error("请选择一条记录");
    }

    /**
     * 新增分润配置
     */
    @RepeatSubmit
    @Log(title = "新增分润配置", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('business:shareProfit:add')")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody KcShareProfitConfig config) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser != null) {
            config.setCreateBy(loginUser.getUsername());
            config.setCreateTime(new Date());
            config.setIsDeleted(Constants.NOT_DELETE);
            return shareProfitConfigService.add(config);
        }
        return AjaxResult.error("操作失败，未获取到当前登陆人信息");
    }


    /**
     * 修改分润配置
     */
    @RepeatSubmit
    @Log(title = "修改分润配置", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('business:shareProfit:update')")
    @PutMapping("/update")
    public AjaxResult update(@Validated @RequestBody KcShareProfitConfig config) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser != null) {
            config.setUpdateBy(loginUser.getUsername());
            config.setUpdateTime(new Date());
            return shareProfitConfigService.update(config, loginUser.getUsername());
        }
        return AjaxResult.error("操作失败，未获取到当前登陆人信息");
    }

    /**
     * 删除分润配置
     */
    @Log(title = "删除分润配置", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('business:shareProfit:delete')")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser != null) {
            return shareProfitConfigService.deleteById(id, loginUser.getUsername());
        }
        return AjaxResult.error("操作失败，无法获取到当前登陆人信息");
    }

    /**
     * 审核分润配置
     */
    @RepeatSubmit
    @Log(title = "审核分润配置", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('business:shareProfit:audit')")
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody KcShareProfitConfig config) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser != null) {
            config.setUpdateBy(loginUser.getUsername());
            config.setUpdateTime(new Date());
            config.setAuditBy(loginUser.getUsername());
            return shareProfitConfigService.audit(config);
        }
        return AjaxResult.error("操作失败，未获取到当前登陆人信息");
    }

}
